package com.atguigu.gmall.rabbit.util;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.rabbit.entity.GmallCorrelationData;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.UUID;

/**
 * @Author: lx
 * @Time: 11:10
 * @Description: 工具类
 * 提交Rabbit的服务 工具类
 */
@SuppressWarnings("all")
@Component
public class RabbitService {


    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private RedisTemplate redisTemplate;

    //发消息（普通）
    public void sendMessage(String exchange, String routingKey, Object message) {

        //为了防止消息发送失败
        GmallCorrelationData correlationData = new GmallCorrelationData();
        //UUID
        String uuid = UUID.randomUUID().toString();
        correlationData.setId(uuid);
        //交换机
        correlationData.setExchange(exchange);
        //路由Key
        correlationData.setRoutingKey(routingKey);
        //消息
        correlationData.setMessage(message);

        //缓存
        redisTemplate.opsForValue().set(uuid, JSONObject.toJSONString(correlationData));
        //发消息
        rabbitTemplate.convertAndSend(exchange, routingKey, message, correlationData);
    }

    //发送死信消息
    public void sendDeadMessage(String exchange, String routingKey, Object message,
                                 int delayTime) {
        GmallCorrelationData correlationData = new GmallCorrelationData();
        //UUID
        String uuid = UUID.randomUUID().toString();
        correlationData.setId(uuid);
        //交换机
        correlationData.setExchange(exchange);
        //路由Key
        correlationData.setRoutingKey(routingKey);
        //消息
        correlationData.setMessage(message);
        //延迟消息
        correlationData.setDelay(true);
        //延迟时间
        correlationData.setDelayTime(delayTime);
        //缓存
        redisTemplate.opsForValue().set(uuid, JSONObject.toJSONString(correlationData));
        //发消息
        rabbitTemplate.convertAndSend(exchange, routingKey, message,
                (msg) -> {
                    //死信过期时间  TTL  ms     6*1000  == 6s
                    msg.getMessageProperties().setExpiration(String.valueOf(delayTime*1000));
                    System.out.println(new Date());
                    return msg;
                },
                correlationData);

    }
    //发送延迟插件消息
    public void sendDelayMessage(String exchange, String routingKey, Object message,
                                int delayTime) {
        GmallCorrelationData correlationData = new GmallCorrelationData();
        //UUID
        String uuid = UUID.randomUUID().toString();
        correlationData.setId(uuid);
        //交换机
        correlationData.setExchange(exchange);
        //路由Key
        correlationData.setRoutingKey(routingKey);
        //消息
        correlationData.setMessage(message);
        //延迟消息
        correlationData.setDelay(true);
        //延迟时间
        correlationData.setDelayTime(delayTime);
        //缓存
        redisTemplate.opsForValue().set(uuid, JSONObject.toJSONString(correlationData));
        //发消息
        rabbitTemplate.convertAndSend(exchange, routingKey, message,
                (msg) -> {
                    //延迟   6*1000  == 6s
                    msg.getMessageProperties().setDelay(delayTime*1000);
                    System.out.println(new Date());
                    return msg;
                },
                correlationData);

    }

}
